package com.pennphoto.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.pennphoto.shared.FieldVerifier;
import com.pennphoto.shared.RegisFieldVerifier;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.user.client.ui.ListBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */

public class Register implements EntryPoint {
	

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	public int userid=-1;
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final RegisterServiceAsync registerService = GWT
			.create(RegisterService.class);

	/**
	 * This is the entry point method.
	 */
	String success="";
	public void onModuleLoad() {
		
		//AbsolutePanel rootPanel = new AbsolutePanel();
		final RootPanel rootPanel = RootPanel.get();
		final Label errorLabel = new Label();
		final ArrayList<String> list = new ArrayList<String>();
		
		
		//this.add(rootPanel);
		rootPanel.setSize("848px", "511px");
		
		HTML html = new HTML("", true);
		rootPanel.add(html);
		
		HTML htmlfirstname = new HTML("<h3>Firstname:<font color=\"red\">*</font></h3", true);
		rootPanel.add(htmlfirstname, 278, 58);
		htmlfirstname.setSize("76px", "22px");
		
		final TextArea textArea = new TextArea();
		rootPanel.add(textArea, 379, 68);
		textArea.setSize("137px", "12px");
		
		
		HTML htmlwelcomeToAlan = new HTML("<h1 align=\"center\"><b><u>Welcome to ALAN's Registration</u></b></h1>", false);
		rootPanel.add(htmlwelcomeToAlan, 245, -17);
		htmlwelcomeToAlan.setSize("530px", "30px");
		final Button btnRegister = new Button("Register");
		rootPanel.add(btnRegister, 508, 481);
		
		HTML html_1 = new HTML("", true);
		rootPanel.add(html_1, 341, 253);
		
		HTML htmllastname = new HTML("<h3>Lastname:<font color=\"red\">*</font></h3", true);
		rootPanel.add(htmllastname, 278, 77);
		htmllastname.setSize("75px", "13px");
		
		final TextArea textArea_1 = new TextArea();
		rootPanel.add(textArea_1, 379, 96);
		textArea_1.setSize("137px", "12px");
		
		HTML htmle = new HTML("<h3>Email ID:<font color=\"red\">*</font></h3", true);
		rootPanel.add(htmle, 583, 91);
		
		HTML htmldateOfBirthmmddyyyy = new HTML("<h3>Date of Birth(MM/DD/YYYY):<font color=\"red\">*</font></h3", true);
		rootPanel.add(htmldateOfBirthmmddyyyy, 144, 115);
		htmldateOfBirthmmddyyyy.setSize("260px", "40px");
		
		HTML htmlgendermf = new HTML("<h3>Gender(M/F):<font color=\"red\">*</font></h3", true);
		rootPanel.add(htmlgendermf, 573, 50);
		htmlgendermf.setSize("95px", "40px");
		
		HTML htmladdress = new HTML("<h3>Address:</h3", true);
		rootPanel.add(htmladdress, 282, 161);
		htmladdress.setSize("59px", "40px");
		
		HTML htmlpassword = new HTML("<h3>Password:<font color=\"red\">*</font></h3", true);
		rootPanel.add(htmlpassword, 583, 147);
		htmlpassword.setSize("76px", "40px");
		
		final TextArea textArea_4 = new TextArea();
		rootPanel.add(textArea_4, 696, 96);
		textArea_4.setSize("137px", "16px");
		
		final TextArea textArea_5 = new TextArea();
		rootPanel.add(textArea_5, 379, 175);
		textArea_5.setSize("137px", "16px");
		
		final PasswordTextBox textArea_6 = new PasswordTextBox();
		rootPanel.add(textArea_6, 696, 161);
		textArea_6.setSize("137px", "16px");
		
		Label label = new Label("");
		rootPanel.add(label, 484, 338);
		
		HTML htmltheMarked = new HTML("<h3>The <font color=\"red\">*</font> marked fields are mandatory</h3>", true);
		rootPanel.add(htmltheMarked, 427, 440);
		
		HTML htmlinstitutionattended = new HTML("<h3>InstitutionAttended 1:</h3>", true);
		rootPanel.add(htmlinstitutionattended, 44, 267);
		
		final TextArea textArea_9 = new TextArea();
		rootPanel.add(textArea_9, 185, 279);
		textArea_9.setSize("137px", "16px");
		
		HTML html_2 = new HTML("", true);
		rootPanel.add(html_2, 339, 397);
		
		HTML htmlinstitutionattended_1 = new HTML("<h3>InstitutionAttended 2:</h3>", true);
		rootPanel.add(htmlinstitutionattended_1, 44, 309);
		
		final TextArea textArea_10 = new TextArea();
		rootPanel.add(textArea_10, 185, 324);
		textArea_10.setSize("137px", "14px");
		
		HTML htmlstudentprofessornormalUser = new HTML("<h3>Student/Professor/User<font color=\"red\">*</font></h3>", true);
		rootPanel.add(htmlstudentprofessornormalUser, 537, 203);
		//rootPanel.setSize("1000px", "700px");
		rootPanel.add(errorLabel);
		errorLabel.setSize("860px", "20px");
		
		final DateBox dateBox = new DateBox();
		rootPanel.add(dateBox, 379, 123);
		
		final ListBox comboBox = new ListBox();
		comboBox.addItem("Male");
		comboBox.addItem("Female");
		rootPanel.add(comboBox, 696, 64);
		
		final ListBox comboBox_1 = new ListBox();
		comboBox_1.addItem("Student");
		comboBox_1.addItem("Professor");
		comboBox_1.addItem("User");
		rootPanel.add(comboBox_1, 721, 217);
		
		HTML htmlstartYear = new HTML("<h3>Start Year:</h3>", true);
		rootPanel.add(htmlstartYear, 350, 267);
		
		HTML htmlstartYear_1 = new HTML("<h3>Start year:</h3>", true);
		rootPanel.add(htmlstartYear_1, 350, 309);
		
		final TextArea str1 = new TextArea();
		rootPanel.add(str1, 434, 281);
		str1.setSize("72px", "12px");
		
		HTML htmlinstitutionattended_2 = new HTML("<h3>End Year:</h3>", true);
		rootPanel.add(htmlinstitutionattended_2, 540, 267);
		htmlinstitutionattended_2.setSize("65px", "38px");
		
		HTML htmlendYear = new HTML("<h3>End Year:</h3>", true);
		rootPanel.add(htmlendYear, 540, 303);
		htmlendYear.setSize("65px", "40px");
		
		final TextArea end1 = new TextArea();
		rootPanel.add(end1, 633, 279);
		end1.setSize("72px", "12px");
		
		final TextArea end2 = new TextArea();
		rootPanel.add(end2, 633, 324);
		end2.setSize("72px", "12px");
		
		final TextArea str2 = new TextArea();
		rootPanel.add(str2, 432, 324);
		str2.setSize("72px", "12px");
		
		HTML htmlmajor = new HTML("<h3>Major:</h3>", true);
		rootPanel.add(htmlmajor, 723, 267);
		htmlmajor.setSize("65px", "38px");
		
		HTML html_3 = new HTML("<h3>Major:</h3>", true);
		rootPanel.add(html_3, 721, 309);
		html_3.setSize("65px", "38px");
		
		final TextArea major2 = new TextArea();
		rootPanel.add(major2, 775, 324);
		major2.setSize("72px", "12px");
		
		final TextArea major1 = new TextArea();
		rootPanel.add(major1, 775, 279);
		major1.setSize("72px", "12px");
		
		Button btnAddAnInterest = new Button("Add an Interest:");
		rootPanel.add(btnAddAnInterest, 245, 217);
		

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		final DialogBox dialogBox1 = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		final Button closeButton1 = new Button("Close");
		final Button Add = new Button("Add");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogrootPanelanel = new VerticalPanel();
		dialogrootPanelanel.addStyleName("dialogrootPanelanel");
		dialogrootPanelanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogrootPanelanel.add(textToServerLabel);
		dialogrootPanelanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogrootPanelanel.add(serverResponseLabel);
		dialogrootPanelanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogrootPanelanel.add(closeButton);
		dialogBox.setWidget(dialogrootPanelanel);
		final String u_type=comboBox_1.getItemText(comboBox_1.getSelectedIndex());
		// Add a handler to close the DialogBox
		
		
		
		// Add a handler to close the DialogBox
				closeButton1.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogBox1.hide();
						}	
				});


		// Create a handler for the Register Button
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the RegisterButton.
			 */
			public void onClick(ClickEvent event) {
				try {
						sendNameToServer();
					
						dialogBox.hide();
						if (success.equals("Registered")){
						if(comboBox_1.getItemText(comboBox_1.getSelectedIndex()).equals("Student")){
							RootPanel.get().clear();
							new Student().onModuleLoad();
							
						}else
						if(comboBox_1.getItemText(comboBox_1.getSelectedIndex()).equals("Professor")){
							RootPanel.get().clear();
							new Professor().onModuleLoad();
							
						}else
						if(comboBox_1.getItemText(comboBox_1.getSelectedIndex()).equals("User")){
							RootPanel.get().clear();
							//Anand's Page
							if (userid!=-1){
								rootPanel.get().clear();
								UserProfile user =new UserProfile(userid);
								user.onModuleLoad();
								
							}
							
						}
						}
						btnRegister.setEnabled(true);
						btnRegister.setFocus(true);
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					dialogBox
					.setText("Remote Procedure Call - Failure");
			serverResponseLabel
					.addStyleName("serverResponseLabelError");
			serverResponseLabel.setHTML(SERVER_ERROR);
			dialogBox.center();
			closeButton.setFocus(true);
				}
				//RootPanel.get().remove(0);
				//RootPanel.get().remove(0);
				//RootPanel.get().clear();
				//RootPanel.get().add(new Pan(nameField.getText()));
				
			}

			/**
			 * Fired when the user types in the Fields.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					try {
						sendNameToServer();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						dialogBox
						.setText("Remote Procedure Call - Failure");
				serverResponseLabel
						.addStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML(SERVER_ERROR);
				dialogBox.center();
				closeButton.setFocus(true);
					}
					//RootPanel.get().clear();
					//RootPanel.get().add(new Pan(nameField.getText()));
					//sendNameToServer();
				}
			}
			
			private void sendNameToServer() throws Exception {
				//System.out.println("Hello");
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = textArea.getText();
				String text1ToServer =textArea_1.getText();
				Date date =dateBox.getDatePicker().getValue();
				System.out.println("Date:"+date);
				String gender=comboBox.getItemText(comboBox.getSelectedIndex()); 
				String text4ToServer =textArea_4.getText();
				String text5ToServer =textArea_5.getText();
				String text6ToServer =textArea_6.getText();
				String text9ToServer =textArea_9.getText();
				String text10ToServer =textArea_10.getText();
				String start1 =str1.getText();
				String start2 =str2.getText();
				String en1 =end1.getText();
				String en2 =end2.getText();
				String mjr1 =major1.getText();
				String mjr2 =major2.getText();
				String usertype=comboBox_1.getItemText(comboBox_1.getSelectedIndex());
				if (RegisFieldVerifier.isValidName(textToServer,text1ToServer,date,text4ToServer,text6ToServer,start1,start2,en1,en2)!=null) {
					String str=RegisFieldVerifier.isValidName(textToServer,text1ToServer,date,text4ToServer,text6ToServer,start1,start2,en1,en2);
					//errorLabel.setText("Please enter at least four characters");
					errorLabel.setText(str);
					return;
				}
				
				// Then, we send the input to the server.
				btnRegister.setEnabled(false);
				textToServerLabel.setText(textToServer);
				textToServerLabel.setText(text1ToServer);
				textToServerLabel.setText(text4ToServer);
				textToServerLabel.setText(text5ToServer);
				textToServerLabel.setText(text6ToServer);
				textToServerLabel.setText(text9ToServer);
				textToServerLabel.setText(text10ToServer);
				textToServerLabel.setText(en1);
				textToServerLabel.setText(en2);
				textToServerLabel.setText(start1);
				textToServerLabel.setText(start2);
				textToServerLabel.setText(mjr1);
				textToServerLabel.setText(mjr2);
				serverResponseLabel.setText("");
	
				registerService.greetServer1(textToServer,text1ToServer,date,gender,text4ToServer,text5ToServer,text6ToServer,text9ToServer,text10ToServer,usertype,en1,en2,start1,start2,mjr1,mjr2,list,						
						new AsyncCallback<Integer>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR+":"+caught.getMessage());
								caught.printStackTrace();
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(Integer result) {
								userid=result;
								//dialogBox.setText("Remote Procedure Call");
								//serverResponseLabel.removeStyleName("serverResponseLabelError");
								//serverResponseLabel.setHTML(result);
								//success=result;
								//dialogBox.center();
								//closeButton.setFocus(true);
								if (result!=-1){
									rootPanel.get().clear();
									
										if(comboBox_1.getItemText(comboBox_1.getSelectedIndex()).equals("Student")){
											RootPanel.get().clear();
											new Student().onModuleLoad();
											
										}else
										if(comboBox_1.getItemText(comboBox_1.getSelectedIndex()).equals("Professor")){
											RootPanel.get().clear();
											new Professor().onModuleLoad();
											
										}else
										if(comboBox_1.getItemText(comboBox_1.getSelectedIndex()).equals("User")){
											RootPanel.get().clear();
											//Anand's Page
											if (userid!=-1){
												rootPanel.get().clear();
												UserProfile user =new UserProfile(userid);
												user.onModuleLoad();
												
											}
										
										}
									
								}else{
									Window.alert("Email id already exists! Please try again!!!");
									textArea_4.setFocus(true);
								}
							}
						});
			}
		}
		//Adding a handler for Add An Interest Button
		class MyHandler1 implements ClickHandler {
			/**
			 * Fired when the user clicks on the RegisterButton.
			 */
			public void onClick(ClickEvent event) {
				try {
					
					dialogBox1.setSize("300", "300");
					dialogBox.setAnimationEnabled(true);
					
					// We can set the id of a widget by accessing its Element
					closeButton1.getElement().setId("closeButton");
					final Label textToServerLabel = new Label();
					final HTML serverResponseLabel = new HTML();
					VerticalPanel dialogrootPanelanel = new VerticalPanel();
					dialogrootPanelanel.addStyleName("dialogrootPanelanel");
					dialogrootPanelanel.add(new HTML("<b>Please add an Interest:</b>"));
					final TextArea interest = new TextArea();
					//final String intr =interest.getText();
					// Add a handler to add an interest
					Add.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							list.add(interest.getText());
							dialogBox1.hide();
							}	
					});
					dialogrootPanelanel.add(interest);
					dialogrootPanelanel.add(serverResponseLabel);
					dialogrootPanelanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
					dialogrootPanelanel.add(Add);
					dialogrootPanelanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
					dialogrootPanelanel.add(closeButton1);
					dialogBox1.setWidget(dialogrootPanelanel);
					dialogBox1.center();
					//sendNameToServer();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					dialogBox
					.setText("Remote Procedure Call - Failure");
			serverResponseLabel
					.addStyleName("serverResponseLabelError");
			serverResponseLabel.setHTML(SERVER_ERROR);
			dialogBox.center();
			closeButton.setFocus(true);
				}
				//RootPanel.get().remove(0);
				//RootPanel.get().remove(0);
				//RootPanel.get().clear();
				//RootPanel.get().add(new Pan(nameField.getText()));
				
			}
		}

		
		
		
		// Add a handler to Register
		MyHandler handler = new MyHandler();
		btnRegister.addClickHandler(handler);
		textArea.addKeyUpHandler(handler);
		textArea_1.addKeyUpHandler(handler);
		comboBox_1.addKeyUpHandler(handler);
		textArea_4.addKeyUpHandler(handler);
		textArea_6.addKeyUpHandler(handler);
		
		MyHandler1 handler1 = new MyHandler1();
		btnAddAnInterest.addClickHandler(handler1);
		
		
				
	}
}
